package redlib.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redlib.backend.annotation.NeedNoPrivilege;
import redlib.backend.annotation.Privilege;
import redlib.backend.dto.ContactsDTO;
import redlib.backend.dto.query.ContactsQueryDTO;
import redlib.backend.model.Page;
import redlib.backend.service.ContactsService;
import redlib.backend.vo.ContactsProvinceVO;
import redlib.backend.vo.ContactsVO;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/contacts")
public class ContactsController {
    @Autowired
    private ContactsService contactsService;

    @PostMapping("listContacts")
    @Privilege("page")
    public Page<ContactsVO> listContacts(@RequestBody ContactsQueryDTO queryDTO) {
        return contactsService.listByPage(queryDTO);
    }

    @PostMapping("addContacts")
    @Privilege("add")
    public Integer addContacts(@RequestBody ContactsDTO contactsDTO) {
        return contactsService.addContacts(contactsDTO);
    }

    @PostMapping("updateContacts")
    @Privilege("update")
    public Integer updateContacts(@RequestBody ContactsDTO contactsDTO) {
        return contactsService.updateContacts(contactsDTO);
    }

    @PostMapping("deleteContacts")
    @Privilege("delete")
    public void deleteContacts(@RequestBody List<Integer> ids) {
        contactsService.deleteByCodes(ids);
    }

    @PostMapping("getContactsByCode")
    @NeedNoPrivilege
    public String getContactsByCode() {
        return "Hello,world";
    }

    @PostMapping("addRecord")
    public void addRecord(@RequestBody ContactsDTO contactsDTO) {
        contactsService.insert(contactsDTO);
    }
    @PostMapping("staProvince")
    public List<ContactsProvinceVO> staContactsProvince() {return contactsService.staContactsProvince();
    }
}
